﻿
using BW.IBLL;
using BW.Model;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.BLL
{
    public class kangfuBLL : IkabgfuBLL
    {
        public kangfuBLL(IbaseDAL<kangfuModel> dal)
        {
            Dal = dal;
        }

        public IbaseDAL<kangfuModel> Dal { get; }

        public List<kangfuModel> daochu()
        {
            return Dal.GetAll().ToList(); 
        }

        public async Task<int> plxiugaizhuangtai(string idstring)
        {
            List<int> idlist=idstring.Split(',').Select(x=>Convert.ToInt32(x)).ToList();
            var prolist = Dal.GetAll().Where(x => idlist.Contains(x.id));
            foreach (var item in prolist)
            {
                item.zhuangtai = item.zhuangtai == 1 ? 2 : 1;
            }
            return await Dal.UpdateRangeAsync(prolist.ToList());
        }

        public Task<int> shanchu(int id)
        {
            var a = Dal.GetAll().FirstOrDefault(a => a.id == id);
            a.isdelt = false;
            return Dal.UpdateAsync(a);
        }

        public Task<int> tinajia(kangfuModel m)
        {
            string a = DateTime.Now.ToString("yyyymmdd");
            string b = new Random().Next(100,1000).ToString();
            m.bianhoa = a+b;
            return Dal.AddAsync(m);
        }

        public dynamic xianshi(string? name, int pageindex, int pagesize)
        {
            var kangfu = Dal.GetAll().Where(a=>a.isdelt==true);

            var rph = from a in kangfu
                      select new
                      {
                          a.id,
                          a.name,
                          a.shichang,
                          a.zhuangtai,
                          a.renshu,
                          a.isdelt,
                          a.bianhoa
                      };

            if (!string.IsNullOrEmpty(name))
            {
                rph = rph.Where(x => x.name.Contains(name));
            }

            var count= rph.Count();
            var list= rph.OrderBy(a=>a.id).Skip((pageindex-1)*pagesize).Take(pagesize).ToList();


            return new
            {
                count = count,
                list = list
            };
        }
    }
}
